﻿using Newtonsoft.Json;
using RobotPerCeption.Entity;
using System.IO;
using System.Windows;

namespace RobotPerCeption.utils
{
    public class FileUtils
    {

        public static string saveFileRootPath { set; get; }


        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool createFileDirectory(string path)
        {

            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (UnauthorizedAccessException e)
                {
                    MessageBox.Show("访问权限不足" + e.Message);
                    return false;
                }
            }


            return false;
        }

        /// <summary>
        /// 写二进制流文件
        /// </summary>
        /// <param name="rootPath">@require true,</param>
        /// <param name="fileName">@require true, must need fileName with extend File  Name,   true</param>
        /// <param name="bytes">@require true,</param>
        /// <returns></returns>
        public static bool writeFileByByte(string rootPath, string fileName, byte[] bytes)
        {

            if (fileName.LastIndexOf(".") <= 0)
            {

                return false;
            }
            FileStream file = null;
            try
            {
                file = new FileStream(rootPath + "/" + fileName, FileMode.OpenOrCreate, FileAccess.Write);

                file.Write(bytes, 0, bytes.Length);

            }
            catch (IOException ioException)
            {
                ioException.GetBaseException();
            }
            finally
            {
                if (file != null)
                {
                    file.Close();
                }
            }
            return true;
        }

        /// <summary>
        /// 文件内容重写,覆盖原本文件内容
        /// </summary>
        /// <param name="rootPath">文件所在根目录路径</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="content">覆盖内容</param>
        /// <returns></returns>
        public static bool overWriteJsonContent(string rootPath, string fileName, string content)
        {
            string filePath = rootPath + "/" + fileName;
            if (File.Exists(filePath))
            {
                try
                {
                    File.WriteAllText(filePath, content);
                    return true;
                }
                catch (IOException exception)
                {
                    return false;
                }

            }
            return true;

        }


        /// <summary>
        /// 文件删除
        /// </summary>
        /// <param name="rootPath"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool delteFile(string rootPath, string fileName)
        {
            string path = rootPath + "/" + fileName;
            if (File.Exists(path))
            {
                File.Delete(path);
                return true;
            }
            return false;

        }


        /// <summary>
        /// 获取不带扩展名的文件名称    
        /// </summary>
        /// <param name="fileName">文件名携带扩展名</param>
        /// <returns></returns>
        public static string getName(string fileName)
        {
            string name = "";
            int index = fileName.LastIndexOf(".");
            if (index > 0)
            {
                name = fileName.Substring(0, index);
                return name;
            }

            return fileName;
        }


        /// <summary>
        /// 读取上传的json文件数据
        /// </summary>
        /// <param name="rootPath"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static RoboteExcuteFile? readUploadFile(string rootPath, string fileName)
        {
            string readFilePath = rootPath + "/" + fileName;
            try
            {
                string result = File.ReadAllText(readFilePath);
                if (!string.IsNullOrEmpty(result))
                {

                    RoboteExcuteFile curObject = JsonConvert.DeserializeObject<RoboteExcuteFile>(result);

                    return curObject;
                }


            }
            catch (Exception excption)
            {

                return null;
            }


            return null;
        }


        /// <summary>
        /// 强制根据文件名称获取对应生成的json文件名称
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string parseFileJsonName(string fileName)
        {
            string name = "";
            if (fileName != null)
            {

                name = getName(fileName);
                name += ".json";

            }
            return name;


        }




    }
}
